import axios from 'axios'
import Qs from 'qs'

// 超时设定
axios.defaults.timeout = 15000
axios.defaults.baseURL = "/sb";

//http request 拦截器
axios.interceptors.request.use(config => {
  return config
}, err => {
  Message({message: '请求超时', type: 'error'})
  return Promise.reject(err)
})

// http response 拦截器
axios.interceptors.response.use(response => {

  const data = response.data;
  if(data.success){//如果返回success，statusCode==200时，不拦截
    return data
  }

  // 根据返回的code值来做不同的处理(和后端约定)
  switch (data.statusCode) {
    case '401':
      console.log('401:'+data.message)
      return Promise.reject('401')
    default:
      console.log('未知错误'+data.message)
      return Promise.reject('未知错误')
  }

}, (err) => {//响应失败时的处理
  return Promise.reject(err)
})

//封装 getRequest
export function  getRequest(url, params) {
  return axios({
    method: 'get',
    url: `${url}`,
    params: params
  })
}

export function  postRequest(url, params) {

  return axios({
    method: 'post',
    url: `${url}`,
    data: params,
    transformRequest: [function (data) {

      return Qs.stringify(data, {allowDots: true})
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}
 //封装 postRequest-json数据格式
 export function  postJsonRequest (url, params)  {

  return axios({
    method: 'post',
    url: `${url}`,
    data: params,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}



